<?php
/**
 * @Project: None
 * @FileName: Role.php
 * @FilePath: D:\thinkphp6\tp\app\controller\Role.php
 * @Author: zishuiliufeng
 * @Date:   2021-01-28 17:21:49
 * @Last Modified by:   zishuiliufeng
 * @Last Modified time: 2021-02-15 21:49:24
 */
namespace app\controller;
use think\Facade\Db;
use app\model\RoleInfo;
use think\facade\Request;
use app\BaseController;
use app\Response;
use app\common;

class Role extends BaseController{
    /**
     * [gameList 获取角色列表]
     * @return [type] [description]
     */
    public function roleList(){
        //静态获取参数
        //$param = Request::param();
        $param = $this->request->param();
        $page = isset($param['page'])?(int)$param['page']:1;
        $perpage = isset($param['perpage'])?(int)$param['perpage']:10;

        $game_id = isset($param['game_id'])?(int)$param['game_id']:1;

        $name = isset($param['name'])?(string)$param['name']:'';

        //筛选条件数组
        $where = [];

        if(!empty($name)){
            $where[] = [
                ['name', 'like', '%'.$name.'%']
            ];
        }

        //$game = Db::name('game')->where('id','=',1)->select();
        //$game = GameModel::where('id','=',1)->select();
        //var_dump($game);
        // 查询状态为1的用户数据 并且每页显示10条数据
        $list = Db::name('role_info')->where('game_id',$game_id)->where($where)->order('id' ,'asc')->paginate($perpage)->each(function($item,$key){
            $http_img = common::getHttpImg($item['img']);
            $item['http_img'] = $http_img;
            
            return $item;
        });
        //当前页数
        $currentPage = $list->currentPage();
        // 获取总记录数
        $total = $list->total();
        //最后页数
        $lastPage = $list->lastPage();
        //每页显示数
        $listRows = $list->listRows();
        //数据
        $data = $list->items();
        
        $result = array(
            'currentPage' => $currentPage,
            'total' => $total,
            'lastPage' => $lastPage,
            'listRows' => $listRows,
            'content' => $data
        );

        
        Response::show(200,'ok',$result);
    }

    /**
     * [getRoleInfo 根据id获取人物信息]
     * @return [json] [description]
     */
    public function getRoleInfo()
    {
        $param = Request::param();
        $id = isset($param['id'])?(int)$param['id']:0;

        $result = array();
        if($id < 1 ){
            $result = array('id'=>$id);
            Response::show(201,'参数错误',$result);
        }

        $role = RoleInfo::where('id',$id)->findOrEmpty();
        if($role->isEmpty())
        {
            Response::show(404,'角色不存在');
        }

        $result['id']           = $role->id;
        $result['game_id']      = $role->game_id;
        $result['name']         = $role->name;
        $result['en_name']      = $role->en_name;
        $result['img']          = $role->img;
        $result['introduction'] = $role->introduction;
        $result['relationship'] = $role->relationship;
        $result['age']          = $role->age;
        $result['gender']       = $role->gender;
        $result['http_img']     = common::getHttpImg($role->img);

        Response::show(200,'ok',$result);
    }
    /**
     * [edit 角色信息编辑]
     * @return [type] [description]
     */
    public function edit(){
        $param = Request::param();
        $id = isset($param['id'])?(int)$param['id']:0;
        $game_id = isset($param['game_id'])?(int)$param['game_id']:0;

        if($id < 1 || $game_id < 1){
            $result = array('id'=>$id,'game_id'=>$game_id);
            Response::show(201,'参数错误',$result);
        }

        $name = isset($param['name'])?(string)$param['name']:'';
        $en_name = isset($param['en_name'])?(string)$param['en_name']:'';
        //角色名称、英文简称不能为空
        if(empty($name) || empty($en_name)){
            Response::show(301,'角色名称与英文简称不能为空');
        }

        //同一游戏内姓名不能重复
        $repeat = RoleInfo::where('name',$name)->where('game_id',$game_id)->findOrEmpty();
        if(!$repeat->isEmpty() && $repeat->id != $id){
            Response::show(300,'角色姓名已存在');
        }

        //同一游戏内英文简称也不能重复
        $repeat_en_name = RoleInfo::where('en_name',$en_name)->where('game_id',$game_id)->findOrEmpty();
        if(!$repeat_en_name->isEmpty() && $repeat_en_name->id != $id){
            Response::show(300,'角色英文简称已存在');
        }

        $age = isset($param['age'])?(int)$param['age']:'';
        $gender = isset($param['gender'])?(string)$param['gender']:'Female';
        $img = isset($param['img'])?(string)$param['img']:'';
        $introduction = isset($param['introduction'])?(string)$param['introduction']:'';
        $relationship = isset($param['relationship'])?(string)$param['relationship']:'';

        //根据id查找数据，并判断数据是否存在
        $role = RoleInfo::where('id',$id)->findOrEmpty();
        if($role->isEmpty()){
            Response::show(404,'数据不存在');
        }
        //存在则修改
        $role->name = $name;
        $role->en_name = $en_name;
        $role->age = $age;
        $role->gender = $gender;
        $role->img = $img;
        $role->introduction = $introduction;
        $role->relationship = $relationship;
        $role->update_time = date('Y-m-d H:i:s');
        $role->save();
        Response::show(200,'修改成功');
    }
    /**
     * [addRole 增加角色]
     */
    public function addRole(){
        $param = Request::param();
        $game_id = isset($param['game_id'])?(int)$param['game_id']:0;

        if($game_id < 1){
            $result = array('game_id'=>$game_id);
            Response::show(201,'参数错误',$result);
        }

        $name = isset($param['name'])?(string)$param['name']:'';
        $en_name = isset($param['en_name'])?(string)$param['en_name']:'';
        //角色名称、英文简称不能为空
        if(empty($name) || empty($en_name)){
            Response::show(301,'角色名称与英文简称不能为空');
        }

        //同一游戏内姓名不能重复
        $repeat = RoleInfo::where('name',$name)->where('game_id',$game_id)->findOrEmpty();
        if(!$repeat->isEmpty()){
            Response::show(300,'角色姓名已存在');
        }

        //同一游戏内英文简称也不能重复
        $repeat_en_name = RoleInfo::where('en_name',$en_name)->where('game_id',$game_id)->findOrEmpty();
        if(!$repeat_en_name->isEmpty()){
            Response::show(300,'角色英文简称已存在');
        }

        $age = isset($param['age'])?(int)$param['age']:'';
        $gender = isset($param['gender'])?(string)$param['gender']:'Female';
        $img = isset($param['img'])?(string)$param['img']:'';
        $introduction = isset($param['introduction'])?(string)$param['introduction']:'';
        $relationship = isset($param['relationship'])?(string)$param['relationship']:'';

        $role = new RoleInfo;
        $role->game_id = $game_id;
        $role->name = $name;
        $role->en_name = $en_name;
        $role->age = $age;
        $role->gender = $gender;
        $role->img = $img;
        $role->introduction = $introduction;
        $role->relationship = $relationship;
        $role->create_time = date('Y-m-d H:i:s');
        $role->update_time = date('Y-m-d H:i:s');
        $role->save();
        //获取自增ID
        $id = $role->id;
        Response::show(200,'新增成功',$id);
    }
    /**
     * [delEvent 删除角色]
     * @return [type] [description]
     */
    public function delRole()
    {
        $param = $this->request->param();
        $id = isset($param['id'])?(int)$param['id']:0;
        //验证角色是否存在
        $event = RoleInfo::where('id',$id)->findOrEmpty();
        if($event->isEmpty()){
            Response::show(404,'事件不存在');
        }

        $del = $event->delete();    //bool事件，删除成功返回true,失败返回false
        if($del){
            $code = 200;
            $msg = '删除成功';
        }else{
            $code = 204;
            $msg = '删除失败';
        }

        Response::show($code,$msg);
    }
    
}